package com.lsh.streamgd.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;

public class FileLocker {
	//private static final String rdfDataName = "ORIENTATION_20130725.rdf";
    private static final String proURI = "http://www.semantic-linking.com/property/";
    private static final String resURI = "http://www.semantic-linking.com/resource/";
	//set a private only lock
	private static ReadWriteLock lock = new ReentrantReadWriteLock();
	
	private static FileLocker instance = null;
	
	protected FileLocker(){
		//exists only to defeat instantiation
	}
	
	public static synchronized FileLocker getInstance(){
		if(instance == null){
			synchronized(FileLocker.class){
				if(instance == null){
					instance = new FileLocker();
				}
			}
		}
		return instance;
	}

	public int writeModel(Model model,String rdfFilePath){
		File f = new File(rdfFilePath);
		Model modelBase;
		//get the lock
		Lock writeLock = lock.writeLock();
		if(!writeLock.tryLock()){		
			try {
				Thread.sleep(3000);
				writeLock.lock();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int flag = 1;
		try{
			//read the old data.rdf 
			if(f.exists()){
				modelBase = FileManager.get().loadModel(rdfFilePath);
			}
			else{
				modelBase = ModelFactory.createDefaultModel();
			}
			Model uniform = modelBase.union(model);
			OutputStream out = new FileOutputStream(f);
			//write and close;
			uniform.write(out,"RDF/XML-ABBREV");
			//uniform.write(out,"TTL");
			out.flush();
			out.close();
		} catch (Exception e){
			System.out.println("Error:"+e.getMessage());
			flag = -1;
		}finally{
			writeLock.unlock();
		}
		return flag;
	}
	
	public int deleteEleFromModel(Model model,String rdfFilePath){
		File f = new File(rdfFilePath);
		Model modelBase;
		//get the lock
		Lock writeLock = lock.writeLock();
		writeLock.lock();
		try{
			//read the old data.rdf 
			if(f.exists()){
				f.delete();
			}
			OutputStream out = new FileOutputStream(f);
			//write and close;
			//uniform.write(out,"RDF/XML-ABBREV");
			model.write(out,"RDF/XML-ABBREV");
			//model.write(out,"TTL");
			out.flush();
			out.close();
		} catch (Exception e){
			System.out.println("Error:"+e.getMessage());
			return -1;
		}finally{
			writeLock.unlock();
		}
		return 1;
	}
	
	//read old data.rdf
	public Model readOntoFile(String fileName) {
		Model model = null;
		File f = new File(fileName);
		Lock readLock = lock.readLock();
		readLock.lock();
		try{
			if(f.exists()){
				model = FileManager.get().loadModel(fileName);
			}
			else{
				model = ModelFactory.createDefaultModel();
			}
		} catch(Exception e){
			System.out.print("readOntoFile:"+ e.getMessage());
		}finally{
			readLock.unlock();
		}
		return model;
	}
	
}
